<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class DeviceInspectOrder extends Model
{
    protected $table = 'inspect_device_order';
    protected $fillable = [
        'inspect_level',
        'inspect_type',
        'device_id',
        'inspect_date',
        'occurrence_time',
        'img_path',
        'remark',
        'operator',
        'record_date',
        'inspector'
    ];

    public function setOccurrenceTimeAttribute($value)
    {
        return $this->attributes['occurrence_time'] = strtotime($value);
    }

    public function getOccurrenceTimeAttribute($value)
    {
        return $this->attributes['occurrence_time'] = date('Y-m-d H:i:s', $value);
    }

    public function setInspectDateAttribute($value)
    {
        return $this->attributes['inspect_date'] = strtotime($value);
    }

    public function getInspectDateAttribute($value)
    {
        return $this->attributes['inspect_date'] = date('Y-m-d H:i:s', $value);
    }

    public function getRecordDateAttribute($value)
    {
        return $this->attributes['record_date'] = date('Y-m-d H:i:s', $value);
    }

    public function getImgPathAttribute($value)
    {
        if (!$value) {
            return [];
        }

        $value = collect(explode(',', $value));

        $newValue = $value->map(function ($item) {
            if ($item) {
                return [
                    'name' => $item,
                    'url' => config('custom.file_visit_url') . '/' . $item
                ];
            }
        });

        return $newValue;
    }
}
